//全局变量
var net = new Object();

//var ajax=new AJAXRequest([url],[callback],[content],[method],[async]);
//下面是其构造函数 
net.AjaxRequest = function (url, onload, onerror, method, params) {
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.loadDate(url, method, params);
};
//定义对象的方法，通过prototype定义方法的好处是每个对象不会有该函数的一个copy
net.AjaxRequest.prototype.loadDate = function (url, method, params) {
	if (!method) {
		method = "GET";
	}
	if (window.XMLHttpRequest) {
		this.req = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (this.req) {
		try {
			var loader = this;//this 指当前函数loadXMLDoc所依附对象即AjaxRequest实例 
			this.req.onreadystatechange = function () {
				net.AjaxRequest.onReadyState.call(loader);//将onReadyState该静态方法依附给loader对象，请注意这种调用方法！
			};
			this.req.open(method, url, true);
      //this.req.send(params);
			this.req.send(null);
		}
		catch (err) {
			this.onerror.call(this);
		}
	}
};
//重构onReadyState函数
net.AjaxRequest.onReadyState = function () {
	var req = this.req;
	var ready = req.readyState;
	if (ready == 4) {
		if (req.status == 200) {
			this.onload.call(this);
		} else {
			this.onerror.call(this);
		}
	}
};
//默认错误处理函数
net.AjaxRequest.prototype.defaultError = function () {
	alert("error fetching data!" + "\n\nreadyState:" + this.req.readyState + "\nstatus: " + this.req.status + "\nheaders: " + this.req.getAllResponseHeaders());
};

